import { getUserInfo } from '@/services/login';
import { useRequest } from 'ahooks';
import { useState } from 'react';

export default function Page() {
  const [user, setUser] = useState<any>({});
  const [cookie, setCookie] = useState(localStorage.getItem('cookie') || false);
  const { run, loading } = useRequest(() => getUserInfo(), {
    manual: true,
    onBefore: () => {
      const {
        lastNameEn,
        firstNameEn,
        firstNameCn,
        lastNameCn,
        xScore = 0,
        xScoreAmount,
        scoreLevelStr,
        email,
        phone,
      } = user || {};

      setUser({
        lastNameEn,
        firstNameEn,
        xScore,
        xScoreAmount,
        scoreLevelStr,
        email,
        phone,
        firstNameCn,
        lastNameCn,
      });
    },
    onSuccess: (data) => {
      setUser(data.data);
    },
  });

  return { user, run, loading, setUser, setCookie, cookie };
}
